//
//  recv_service_reply.hpp
// ~~~~~~~~~~~~~~~~~~~~~
//
// Copyright (c) 2013-2014 Razvan Coca (razvan dot coca at  gmail dot com)
// This is mostly generated code. Generator is distributed under GPL. Edit it to suit your needs.
// 


#ifndef recv_service_reply_hpp
#define recv_service_reply_hpp
#include <boost/bind.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <boost/asio.hpp>

#include "patterns/command.hpp"
#include "patterns/handlers.hpp"

#include "logging/logging.hpp"

namespace pcmd=patterns::command;
namespace phdl=patterns::handlers;
namespace ba=boost::asio;
namespace bs=boost::system;
namespace bt=boost::posix_time;

#include "components/defs/util_types.hpp"
#include "components/defs/client_processor.hpp"

namespace components{
namespace network{



class recv_service_reply :public boost::enable_shared_from_this<recv_service_reply>, private boost::noncopyable
{
private:
    std::string name_;

    ba::io_service& ios_;

    ba::ip::udp::socket& msocket_;

    recv_service_reply (ba::io_service& ios, ba::ip::udp::socket& msocket) ;

public:
    typedef boost::shared_ptr<recv_service_reply> pointer;

    bool cancel () ;

    static boost::shared_ptr<recv_service_reply> create (ba::io_service& ios, ba::ip::udp::socket& msocket) ;

    virtual  ~recv_service_reply () ;

 template<typename Handler >
      bool exec(udp_endpoint_t& peer, bytes_vector_t& recv_buf, Handler& handler) {
        try{
            LOG(INFO)<<name_<<"::"<<__FUNCTION__;
            recv_buf.resize(std::numeric_limits<ushort>::max());
            ba::mutable_buffers_1 buf=ba::buffer(&recv_buf[0],recv_buf.size());
            msocket_.async_receive_from(buf,peer,handler);
            return true;
        }
        catch(std::exception &e){
           LOG(WARNING)<<name_<<"::"<<__FUNCTION__<<":"<<e.what();
           cancel();
           return false;
        }
        
    }
};

}//end namespace network
}//end namespace components

#endif /*#ifdef recv_service_reply_hpp*/
